package com.tomehay.termprojectmanage_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tomehay.termprojectmanage_server.enums.ResponseCode;
import com.tomehay.termprojectmanage_server.mapper.EvaluationsMapper;
import com.tomehay.termprojectmanage_server.mapper.PersonalTaskMapper;
import com.tomehay.termprojectmanage_server.pojo.Model.Evaluations;
import com.tomehay.termprojectmanage_server.pojo.Model.GroupMember;
import com.tomehay.termprojectmanage_server.mapper.GroupMemberMapper;
import com.tomehay.termprojectmanage_server.pojo.Model.PersonalTask;
import com.tomehay.termprojectmanage_server.pojo.VO.ResultVO;
import com.tomehay.termprojectmanage_server.service.GroupMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tomehay.termprojectmanage_server.utils.StrategyUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tomehay
 * @since 2023-06-20
 */
@Service
public class GroupMemberServiceImpl extends ServiceImpl<GroupMemberMapper, GroupMember> implements GroupMemberService {

    @Resource
    private EvaluationsMapper evaluationsMapper;
    @Resource
    private PersonalTaskMapper taskMapper;

    @Resource
    GroupMemberMapper groupMemberMapper;

    @Value("${filePath}")
    private String filePath;

    /**
     * 互评可拓展为多种任务互评
     * @param evaluations
     */
    @Override
    public void evaluateGroupTask(Evaluations evaluations) {
        evaluationsMapper.insert(evaluations);
    }

    @Override
    public void submitTaskByUpload(MultipartFile file) {
        try {
            //判断文件夹是否存在，不存在时，创建文件夹
            File directoryFile = new File(filePath);
            if (!directoryFile.exists()) {
                //创建多个文件夹
                directoryFile.mkdirs();
            }

            //判断文件是否为空，不为空时，保存文件
            if (!file.isEmpty()) {
                //获取文件名
                String name = file.getOriginalFilename();
                file.transferTo(new File(filePath + name));
                taskMapper.updateDocument(filePath + name);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void submitTaskByChoose(List<PersonalTask> personalTask) {

    }

    @Override
    public ResultVO<GroupMember> getGroupMemberByGroupIdAndStudentId(int groupId, int studentId) {
        QueryWrapper<GroupMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_id",groupId);
        queryWrapper.eq("student_id",studentId);

        List<GroupMember> groupMembers = groupMemberMapper.selectList(queryWrapper);



        if (groupMembers.size()==1){
            GroupMember groupMember = groupMembers.get(0);
            return new ResultVO<>(200,"success",groupMember);
        }

        return new ResultVO<>(ResponseCode.INTERNAL_SERVER_ERROR);
    }
}
